package com.credit.mola.app.utils.analysis.service

import com.adjust.sdk.Adjust
import com.adjust.sdk.Util
import com.google.firebase.messaging.FirebaseMessaging
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage

class SikadwaFirebaseTypeService: FirebaseMessagingService() {

    override fun onCreate() {
        super.onCreate()
        // Fetch FCM token and set in Adjust SDK
        FirebaseMessaging.getInstance().token
            .addOnCompleteListener { task ->
                if (task.isSuccessful && task.result != null) {
                    Adjust.setPushToken(task.result, applicationContext)
                }
            }
    }

    override fun onNewToken(token: String) {
        super.onNewToken(token)

        // Receive new FCM token and set in Adjust SDK
        Adjust.setPushToken(token, applicationContext)
    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)

        // Check if message has data payload
        if (remoteMessage.data.isNotEmpty()) {
            val payload: Map<String, String> = remoteMessage.data

            // Check for Adjust uninstall detection data message
            if (Util.isAdjustUninstallDetectionPayload(payload)) {
                // No handling required for Adjust data payload
            } else {
                // Handle other data payloads here
            }
        }

        // Check if message has notification payload
        remoteMessage.notification?.let {
            // Handle notification payload here
        }
    }
}